package com.JohnCarloSalter.bestbuy_rebates;

import com.JohnCarloSalter.ReceiptDecorator.ReceiptDecorator;
import com.JohnCarloSalter.Receipt.*;

public class RebateDecorator extends ReceiptDecorator {

	private Receipt r;
	
	public RebateDecorator(ReceiptComponent trailer, Receipt appliedReceipt) {
		super.create(trailer);
		r = appliedReceipt;
	}
	
	//Empty trailer constructor.
	public RebateDecorator(Receipt appliedReceipt) {
		r = appliedReceipt;
	}
	
	public void print() {
		super.callTrailer();
		for(int i=0; i<getNumRebates(); i++) {
			System.out.println("\n**********REBATE!**********");
			System.out.println("Bring this receipt to a participating Best Buy store for a $10 rebate!");
			System.out.println("***************************\n");
		}
	}
	
	//We will award $10 rebates on any item with a price over $100.
	public boolean applies() {
		Purchases p = r.getPurchases();
		for(int i = 0; i<p.getNumberOfItems(); i++) {
			if(p.getPurchase(i).getPrice() >= 100.00) {
				return true;
			}
		}
		return false;
	}
	
	private int getNumRebates() {
		int numRebatesApplicable = 0;
		Purchases p = r.getPurchases();
		for(int i = 0; i<p.getNumberOfItems(); i++) {
			if(p.getPurchase(i).getPrice() >= 100.00) {
				numRebatesApplicable++;
			}
		}
		return numRebatesApplicable;
	}

}
